package com.momomian.common.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
/**
 * @author momomian
 * @date 2021/12/30
 *
 */
@Slf4j
public class IpUtil {




    /**
     * 获取自定义对象的用户Ip
     */
    public static String getUserIp(HttpServletRequest request) {
        // 获取客户端前台IP进行解析
        String userIp = request.getHeader("x-forwarded-for");
        if (userIp == null || userIp.length() == 0 || "unknown".equalsIgnoreCase(userIp)) {
            userIp = request.getHeader("Proxy-Client-IP");
        }
        if (userIp == null || userIp.length() == 0 || "unknown".equalsIgnoreCase(userIp)) {
            userIp = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(userIp) || "unknown".equalsIgnoreCase(userIp)) {
            userIp = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isEmpty(userIp) || "unknown".equalsIgnoreCase(userIp)) {
            userIp = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (userIp == null || userIp.length() == 0 || "unknown".equalsIgnoreCase(userIp)) {
            userIp = request.getRemoteAddr();
        }
        return userIp;
    }

}
